﻿using Mecha.EdgeCompute.Framework;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ReadData
{
    class Program
    {
        static void Main(string[] args)
        {
            //D d = new D();
            //d.Func();


            //C c = new C();
            //c.Func();

            //ControlableThread thread = new ControlableThread(null, null, null, 30, null);

            //thread.Start();

            //Console.ReadKey();

            var sss = GetLastCycle();
        }

        public static int GetLastCycle()
        {

            int lastCycle = 0;
            int oldCycle = 0;
            string fullPath;
            string path;
            //fullPath = string.Format(@"{0}\{1}-{2}\{3}", FilePath, m_PlanFlow + "-" + TimeStamp, m_AreaName, FileName);
            fullPath = "D://Zone1.csv";// m_ConfigFileName;

            if (File.Exists(fullPath))
            {
                try
                {
                    using (StreamReader sr = new StreamReader(fullPath))
                    {
                        while (!sr.EndOfStream)
                        {
                            if (sr.ReadLine().Contains("[Data]"))
                            {
                                sr.ReadLine();
                                sr.ReadLine();
                                break;
                            }
                        }
                        while (!sr.EndOfStream)
                        {
                            string line = sr.ReadLine();
                            string[] parts = line.Split(',');
                            lastCycle = ObjToInt(parts[0], 1);

                            if (lastCycle > oldCycle)
                            {
                                oldCycle = lastCycle;
                            }
                            else
                            {
                                lastCycle = oldCycle;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                }
            }
            return lastCycle;
        }


        public static int ObjToInt(object expression, int defValue)
        {
            if (expression != null)
                return StrToInt(expression.ToString(), defValue);

            return defValue;
        }

        /// <summary>
        /// 将字符串转换为Int32类型
        /// </summary>
        /// <param name="expression">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int StrToInt(string expression, int defValue)
        {
            if (string.IsNullOrEmpty(expression) || expression.Trim().Length >= 11 || !Regex.IsMatch(expression.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
                return defValue;

            int rv;
            if (Int32.TryParse(expression, out rv))
                return rv;

            return Convert.ToInt32(StrToFloat(expression, defValue));
        }

        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static float StrToFloat(string expression, float defValue)
        {
            if ((expression == null) || (expression.Length > 10))
                return defValue;

            float intValue = defValue;
            if (expression != null)
            {
                bool IsFloat = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
                if (IsFloat)
                    float.TryParse(expression, out intValue);
            }
            return intValue;
        }

        //IEnumerable<DataTable> GetFileData(string sourceFileFullName)
        //{

        //    int chunkRowCount = 0;

        //    using (var sr = new StreamReader(sourceFileFullName))
        //    {
        //        string line = null;
        //        //Read and display lines from the file until the end of the file is reached.                
        //        while ((line = sr.ReadLine()) != null)
        //        {
        //            chunkRowCount++;
        //            var chunkDataTable = ; ////Code for filling datatable or whatever   

        //            if (chunkRowCount == 10000)
        //            {
        //                chunkRowCount = 0;
        //                yield return chunkDataTable;
        //                chunkDataTable = null;
        //            }
        //        }
        //    }
        //    //return last set of data which less then chunk size
        //    if (null != chunkDataTable)
        //        yield return chunkDataTable;
        //}
    }
}
